package com.teamwork.demo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.teamwork.demo.entity.Group;
import com.teamwork.demo.entity.Stu;
import com.teamwork.demo.entity.Application;
import com.hit.demo1.param.StuParam;
import com.teamwork.demo.mapper.A_Mapper;
import com.teamwork.demo.mapper.G_Mapper;
import com.teamwork.demo.mapper.StuMapper;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Predicate;
import jakarta.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdminService implements com.teamwork.demo.service.AdminService {
    @Autowired
    public StuMapper stuMapper;

    @Override
    public Page<Stu> findAll(StuParam stuParam, int page, int limit) {
        Specification<Stu> spe = new Specification<Stu>() {
            @Override
            public jakarta.persistence.criteria.Predicate toPredicate(Root<Stu> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

                List<Predicate> list = new ArrayList<>();
                if (ObjectUtil.isNotEmpty(stuParam.getS1())) {
                    list.add(cb.and(cb.equal(root.get("sname"), stuParam.getS1())));
                }
//                if (ObjectUtil.isNotEmpty(stuParam.getS2())) {
//                    if (ObjectUtil.isNotEmpty(stuParam.getIsLike())) {
//                        list.add(cb.and(cb.like(root.get("saddress"), "%" + stuParam.getS2() + "%")));
//                    } else {
//                        list.add(cb.and(cb.equal(root.get("saddress"), stuParam.getS2())));
//                    }
//                }
                if (ObjectUtil.isNotEmpty(stuParam.getSgroup())) {
                    list.add(cb.and(cb.equal(root.get("sgroup"), stuParam.getSgroup())));
                }

                if (ObjectUtil.isNotEmpty(stuParam.getSgrade())) {
                    list.add(cb.and(cb.equal(root.get("sgrade"), stuParam.getSgrade())));
                }

                if (ObjectUtil.isNotEmpty(stuParam.getSmajor())) {
                    list.add(cb.and(cb.equal(root.get("sclass"), stuParam.getSmajor())));
                }
                if (ObjectUtil.isNotEmpty(stuParam.getSid())) {
                    list.add(cb.and(cb.equal(root.get("sid"), stuParam.getSid())));
                }
                jakarta.persistence.criteria.Predicate[] p = new jakarta.persistence.criteria.Predicate[list.size()];
                return cb.and(list.toArray(p));

            }
        };
        return stuMapper.findAll(spe, PageRequest.of(page, limit));
    }

    @Override
    public Stu save(Integer sid, String sgrade, String sgroup) {
        Stu stu = stuMapper.findById(sid).orElse(null);
        if (stu != null) {

            // 仅更新特定字段
            if (sgrade != null) {
                stu.setSgrade(sgrade);
            }
            if (sgroup != null) {
                stu.setSgroup(sgroup);
            }
            return stuMapper.save(stu);

        } else {
            throw new IllegalArgumentException("学生 " + sid + "不存在");
        }
    }

    @Override
    public Stu update(Stu stu) {
        Stu oldSut = stuMapper.findById(stu.getSid()).orElse(null);
        stu.setSgroup(oldSut.getSgroup());
        stu.setSgrade(oldSut.getSgrade());

        return stuMapper.save(stu);
    }
    @Autowired
    public A_Mapper aMapper;


//    public Application update(Application application) {
//        Application oldSut = aMapper.findById(application.getId()).orElse(null);
//        if (null == oldSut) {
//            return null;
//        }
//        application.setScondition(oldSut.getScondition());
//
//        return aMapper.save(application);
//    }
@Override
    public Application savecon(Integer sid, String scondition) {
        Application application1 = aMapper.findById(sid).orElse(null);
        if (application1 != null) {

            // 仅更新特定字段
            if (scondition != null) {
                application1.setScondition(scondition);
            }
            return aMapper.save(application1);

        } else {
            throw new IllegalArgumentException("学生 " + sid + "不存在");
        }
    }

    @Override
    public int delete(Integer id) {
        stuMapper.deleteById(id);
        return 1;
    }

    @Autowired
    public G_Mapper gMapper;

    @Override
    public Group createGroup(Integer gid, String member1, String member2, String member3, String member4, String member5) {
        Group group = new Group();
        group.setId(gid);
        group.setMember1(member1);
        group.setMember2(member2);
        group.setMember3(member3);
        group.setMember4(member4);
        group.setMember5(member5);

        return gMapper.save(group);
    }

}
